import { _decorator, Component, Node, Button, Label, tween, Vec3 } from 'cc';
import { GameManager } from '../core/GameManager';
import { director } from 'cc';

const { ccclass, property } = _decorator;

/**
 * 游戏结束面板组件
 * 负责显示游戏结束界面和处理相关交互
 */
@ccclass('GameOverPanel')
export class GameOverPanel extends Component {
    @property(Label)
    private scoreLabel: Label = null;
    
    @property(Label)
    private highScoreLabel: Label = null;
    
    @property(Button)
    private restartButton: Button = null;
    
    @property(Button)
    private menuButton: Button = null;
    
    @property({type: String, tooltip: "分数前缀文本"})
    private scorePrefix: string = "得分: ";
    
    @property({type: String, tooltip: "最高分前缀文本"})
    private highScorePrefix: string = "最高分: ";
    
    // 生命周期函数
    onLoad() {
        // 注册按钮事件
        if (this.restartButton) {
            this.restartButton.node.on(Button.EventType.CLICK, this.onRestartGame, this);
        }
        
        if (this.menuButton) {
            this.menuButton.node.on(Button.EventType.CLICK, this.onBackToMenu, this);
        }
        
        // 初始化时隐藏面板
        this.node.active = false;
    }
    
    onDestroy() {
        // 移除事件监听，先检查对象和节点是否有效
        if (this.restartButton && this.restartButton.isValid && this.restartButton.node && this.restartButton.node.isValid) {
            this.restartButton.node.off(Button.EventType.CLICK, this.onRestartGame, this);
        }
        
        if (this.menuButton && this.menuButton.isValid && this.menuButton.node && this.menuButton.node.isValid) {
            this.menuButton.node.off(Button.EventType.CLICK, this.onBackToMenu, this);
        }
    }
    
    /**
     * 显示游戏结束面板
     */
    public show(): void {
        // 显示面板
        this.node.active = true;
        
        // 更新分数显示
        if (GameManager.instance) {
            const score = GameManager.instance.score;
            const highScore = this.getHighScore();
            
            if (this.scoreLabel) {
                this.scoreLabel.string = this.scorePrefix + score;
            }
            
            if (this.highScoreLabel) {
                this.highScoreLabel.string = this.highScorePrefix + highScore;
            }
        }
        
        // 播放显示动画
        this.node.scale = new Vec3(0.5, 0.5, 1);
        tween(this.node)
            .to(0.3, { scale: new Vec3(1, 1, 1) }, {
                easing: 'bounceOut'
            })
            .start();
    }
    
    /**
     * 隐藏游戏结束面板
     */
    public hide(): void {
        // 播放隐藏动画
        tween(this.node)
            .to(0.2, { scale: new Vec3(0.5, 0.5, 1) }, {
                easing: 'backIn'
            })
            .call(() => {
                this.node.active = false;
            })
            .start();
    }
    
    /**
     * 重新开始游戏
     */
    private onRestartGame(): void {
        // 重新加载游戏场景
        director.loadScene('GameScene');
    }
    
    /**
     * 返回主菜单
     */
    private onBackToMenu(): void {
        // 加载主菜单场景
        director.loadScene('MainMenuScene');
    }
    
    /**
     * 获取最高分
     */
    private getHighScore(): number {
        try {
            const savedScore = localStorage.getItem('match3_highscore');
            return savedScore ? parseInt(savedScore) : 0;
        } catch (e) {
            console.error("获取最高分失败:", e);
            return 0;
        }
    }
} 